package com.itheima.service.impl;

import com.itheima.mapper.PermissionMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Set;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    //根据用户名查询数据库中用户信息
    public User findByUsername(String username) {
        //获取用户信息
        User user = mapper.findByUsername(username);
        if (user == null){
            return null;
        }
        Integer userId = user.getId();
        //查询用户角色
        Set<Role> roles = roleMapper.getroleByUserId(userId);
        user.setRoles(roles);//用户关联角色

        for (Role role : roles) {
            Integer roleId = role.getId();
            //获取角色权限
            Set<Permission> permissions = permissionMapper.getPermissionByRoleId(roleId);
            role.setPermissions(permissions);//角色关联权限
        }
        return user;
    }


}
